package com.roy.royminidao.common.util;

/**
 * 
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 * 
 * @author rong
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class StringUtil {

    /**
     * 根据要求字符串长度，数字左补零
     * 
     * @author YuanBing
     * @param data
     * @param length
     * @return
     */
    public static String leftFillZero(String s, int length) {
        if (null == s || "".equals(s.trim()) || length <= 0) {
            return "";
        }
        s = s.trim();
        if (s.length() == length) {
            return s;
        }
        int i = 0;
        StringBuilder sb = new StringBuilder();
        while (i < length) {
            sb.append("0");
            i++;
        }
        sb.append(s);
        return sb.substring(sb.length() - length);
    }

    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static String replace(String text, String searchString, String replacement) {
        return replace(text, searchString, replacement, -1);
    }

    public static String replace(String text, String searchString, String replacement, int max) {
        if (isEmpty(text) || isEmpty(searchString) || replacement == null || max == 0) {
            return text;
        }
        int start = 0;
        int end = text.indexOf(searchString, start);
        if (end == -1) {
            return text;
        }
        int replLength = searchString.length();
        int increase = replacement.length() - replLength;
        increase = increase < 0 ? 0 : increase;
        increase *= max < 0 ? 16 : max > 64 ? 64 : max;
        StringBuilder buf = new StringBuilder(text.length() + increase);
        while (end != -1) {
            buf.append(text.substring(start, end)).append(replacement);
            start = end + replLength;
            if (--max == 0) {
                break;
            }
            end = text.indexOf(searchString, start);
        }
        buf.append(text.substring(start));
        return buf.toString();
    }

    /**
     * 
     * 功能描述: <br>
     * 判断字符串是否为null或""
     *
     * @param s
     * @return
     * @see
     * @since 2.0
     */
    public static boolean isNotNull(String s) {
        if (null != s && s.trim().length() != 0) {
            return true;
        }
        return false;
    }

    /**
     * 判断对象是否为空
     * 
     * @param str
     * @return
     */
    public static boolean isNotEmpty(Object str) {
        boolean flag = true;
        if (str != null && !str.equals("")) {
            if (str.toString().length() > 0) {
                flag = true;
            }
        } else {
            flag = false;
        }
        return flag;
    }

    /**
     * 
     * 功能描述:判断字符串是否为空 <br>
     * true：null、 “”、“ ” 否则为 false
     *
     * @param str
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    public static boolean isAllNull(String[] strs) {
        boolean flag = true;
        for (int i = 0; i < strs.length; i++) {
            if ("".equals(strs[i]) || strs[i] == null) {
                flag = false;
            }
        }
        return flag;

    }
}
